<template>
  <page-card title="定时任务" :need-back="true">
    <ScheduleEdit :jobId="jobId"
                  :back="back"
                  :submit="submit"
                  :extraValidateForm="extraValidateForm"
                  :businessId="businessId">

      <template #taskBasicComp="{ value, fn }">
        <ScheduleBasic @input="fn" :value="value" :appNameList="appNameList" />
      </template>

      <template #taskTypeComp="{ value, fn }">
        <a-divider orientation="left">任务类型</a-divider>
        <ScheduleType @input="fn" :value="value" />
      </template>

      <template #TaskRecipientComp="{ value, fn }">
        <ScheduleRecipient @input="fn" :value="value" />
      </template>
    </ScheduleEdit>
  </page-card>
</template>

<script>
import { ZOkButton } from '@zh/components-ant'

import ScheduleEdit from '@zh/base-ant/framework/views/schedule/components/ScheduleEdit.vue'
// 任务类型
import ScheduleType from '@zh/base-ant/framework/views/schedule/components/ScheduleType.vue'
// 任务基本信息
import ScheduleBasic from '@zh/base-ant/framework/views/schedule/components/ScheduleBasic.vue'
// 任务接收
import ScheduleRecipient from '@zh/base-ant/framework/views/schedule/components/ScheduleRecipient.vue'

import { jobManageAdd, jobManageUpdate, jobgroup } from '../../api/schedule'

export default {
  name: 'CronJobSetting',
  components: {
    ZOkButton,
    ScheduleType,
    ScheduleRecipient,
    ScheduleBasic,
    ScheduleEdit
  },
  data () {
    return {
      appNameList: [],
      jobId: this.$route.query.jobId || '',
      businessId: this.$route.query.businessId || ''
    }
  },

  created () {
    this.getAppNameList()
  },
  methods: {
    getAppNameList () {
      jobgroup().then(res => {
        this.appNameList = res
      })
    },
    back() {
      this.$router.go(-1)
    },
    submit(form, back) {
      // 增加jsonvalue回显
      const { applicationId, applicationType, taskType, applicationMenuName, appName, connectorInterfaceId, connectorId, ...other } = form
      form = {
        ...other,
        appName,
        jsonValue: JSON.stringify({
          applicationId,
          applicationType,
          taskType,
          applicationMenuName,
          appName,
          connectorInterfaceId,
          connectorId
        })
      }
      // 把connectorId和connectorInterfaceId变为int
      if (form?.taskType == '2') {
        // form.connectorId = parseInt(form.connectorId)
        // form.connectorInterfaceId = parseInt(form.connectorInterfaceId)
        form.businessId = connectorInterfaceId
      }
      if (form.id) {
        jobManageUpdate(form).then((res) => {
          // back()
        })
      } else {
        jobManageAdd(form).then((res) => {
          back()
        })
      }
    },
    extraValidateForm(form) {
      return true
    }
  }

}
</script>
<style lang="less" scoped>
</style>